# /usr/bin/env python3.6
# -*- encoding: utf-8 -*-
"""
@File    : moive_hall_api.py
@Time    : 2020/2/8 0008 14:08
@Author  : zhangbing
@Email   : 18829172841@163.com
@Software: PyCharm
"""
from flask_restful import Resource, reqparse, marshal, fields, abort

from App.apis.api_constant import HTTP_OK
from App.models.cinema_admin.cinema_address_model import CinemaAddress
from App.models.cinema_admin.cinema_hall_model import Hall
from App.models.cinema_admin.cinma_hall_movie_model import HallMovie

'''
此接口用于获取相关的影片相关信息
'''
parse = reqparse.RequestParser()

parse.add_argument("movie_id", type=int, help="请输入影片")

parse.add_argument("address_id", type=int, help="请输入影院")

parse.add_argument("district", type=str, help="请输入对应区域")

moviehall = {
    "h_movie_id": fields.Integer,
    "h_hall_id": fields.Integer,
    "h_time": fields.DateTime
}

multi_hall_movie_fileds = {
    "msg": fields.String,
    "status": fields.Integer,
    "data": fields.List(fields.Nested(moviehall))
}


class UserMovieHallsResource(Resource):

    def get(self):
        args = parse.parse_args()

        movie_id = args.get("movie_id")

        address_id = args.get("address_id")

        district = args.get("district")

        cinema_address = CinemaAddress.query.filter(CinemaAddress.district == district).filter(
            CinemaAddress.id == address_id).first()  # 此处通过对应的影片ID和影院区域来获取对应的电影id
        if not cinema_address:
            abort(404, msg="该排挡不存在影片")
        halls = Hall.query.filter_by(h_address_id=cinema_address.id).all()
        all_hall_movies = []
        for hall in halls:
            hall_movies = HallMovie.query.filter_by(h_hall_id=hall.id).filter_by(h_movie_id=movie_id).all()
            all_hall_movies += hall_movies

        data = {
            "msg": "ok",
            "status": HTTP_OK,
            "data": all_hall_movies
        }
        return marshal(data, multi_hall_movie_fileds)


if __name__ == "__main__":
    pass
